package com.myzl.coal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.myzl.coal.client.CacheData;
import com.myzl.coal.domain.RealTimeData;
import com.myzl.coal.mongo.component.MongoComponent;
import com.myzl.coal.mongo.domain.HistoryData;
import com.myzl.coal.service.ICacheBasicService;
import com.myzl.coal.service.IDeviceService;
import com.myzl.coal.service.IDoubleOrderFlowService;
import com.myzl.coal.service.IRealTimeDataService;
import com.myzl.common.core.domain.entity.SysDictData;
import com.myzl.common.enums.DeviceCommandEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author cew
 * @date 2023年11月9日16:43:41
 */
@Slf4j
@Data
@Service
public class DoubleOrderFlowServiceImpl implements IDoubleOrderFlowService {

    private final MongoComponent mongoComponent;
    private final IDeviceService deviceService;
    private final ICacheBasicService cacheService;
    private final IRealTimeDataService realTimeDataService;

    @Override
    public void handleFlow(HistoryData volumeHistorydata) {
        //查询密度集合
        Sort descSort = Sort.by(Sort.Order.desc("createTime"));
        List<HistoryData> densityList = getDensityList(volumeHistorydata.getDeviceId(), descSort);
        HistoryData flowHistoryDataItem = getFlowHistoryDataItem(volumeHistorydata);
        BigDecimal volumeBigdecimal = new BigDecimal(volumeHistorydata.getDataValue());
        if (densityList.isEmpty()){
            // todo 保留五位小数
            flowHistoryDataItem.setDataValue("0.00000");
            updateRealTimeData(flowHistoryDataItem);
            mongoComponent.insertOne(flowHistoryDataItem);
            return;
        }
        HistoryData densityData = densityList.get(0);
        BigDecimal desityBigdecimal = new BigDecimal(densityData.getDataValue());
        if (densityList.size() > 1){
            Map<String, Object> cacheMap = cacheService.cacheBasicData(volumeHistorydata.getDeviceId(), densityList);
            //BigDecimal basicDensity = (BigDecimal) cacheMap.get("density");
            //BigDecimal preDensity = new BigDecimal(densityList.get(1).getDataValue());
            String basicId = String.valueOf(cacheMap.get("densityId"));
            String preId = densityList.get(1).getId();
            if (!basicId.equals(preId)){
                cacheMap = cacheService.updateCacheBasicData(volumeHistorydata.getDeviceId(), densityList);
            }
            BigDecimal basicVolumeBigdecimal = (BigDecimal) cacheMap.get("volume");
            BigDecimal basicFlowBigdecimal = (BigDecimal) cacheMap.get("flow");
            BigDecimal volumeResult = volumeBigdecimal.subtract(basicVolumeBigdecimal);
            BigDecimal flowBigdecimal = volumeResult.multiply(desityBigdecimal);
            // todo 保留五位小数
            BigDecimal flowResult = flowBigdecimal.add(basicFlowBigdecimal).setScale(5, RoundingMode.HALF_UP);
            flowHistoryDataItem.setDataValue(flowResult.toString());
        } else {
            // todo 保留五位小数
            BigDecimal flowBigdecimal = volumeBigdecimal.multiply(desityBigdecimal).setScale(5, RoundingMode.HALF_UP);
            flowHistoryDataItem.setDataValue(flowBigdecimal.toString());
        }
        updateRealTimeData(flowHistoryDataItem);
        mongoComponent.insertOne(flowHistoryDataItem);
    }

    private boolean updateRealTimeData(HistoryData historyData){
        RealTimeData realTimeData = new RealTimeData();
        BeanUtil.copyProperties(historyData,realTimeData);
        int i = realTimeDataService.updateRealTimeDataByType(Collections.singletonList(realTimeData));
        return i > 0;
    }
    private HistoryData getFlowHistoryDataItem(HistoryData item) {
        HistoryData flowHistoryDataItem = new HistoryData();
        Map<String, SysDictData> dictMap = CacheData.getStringSysDictDataMap();
        SysDictData sysDictData = dictMap.get(String.valueOf(DeviceCommandEnum.CUMULATIVE_FLOW.getCode()));
        BeanUtil.copyProperties(item,flowHistoryDataItem);
        flowHistoryDataItem.setDictValue(DeviceCommandEnum.CUMULATIVE_FLOW.getCode());
        flowHistoryDataItem.setDataUnit(sysDictData.getValueUnit());
        flowHistoryDataItem.setId(null);
        return flowHistoryDataItem;
    }

    private List<HistoryData> getDensityList(Integer deviceId,  Sort descSort) {
        List<HistoryData> densityList;
        Criteria criteria = new Criteria();
        criteria.andOperator(
                Criteria.where("deviceId").is(deviceId),
                Criteria.where("dictValue").is(DeviceCommandEnum.CURRENT_DENSITY.getCode())
        );
        densityList = mongoComponent.selectList(criteria, descSort, HistoryData.class);
        return densityList;
    }

}
